package com.lsfb.signln.Location.LocationSearch.ReplenishSignIn;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.My.MyDetailsBean;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbTimePicker;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;

import java.util.HashMap;

/**
 * Created by Administrator on 2018/1/24 0024.
 */
@ContentView(R.layout.aty_replenish_signin)
public class ReplenishSignInActivity extends LsfbActivity {

    /**
     * 时间
     */
    @ViewInject(R.id.aty_replenish_text_time)
    private TextView text_time;

    /**
     * 原因
     */
    @ViewInject(R.id.aty_replenish_text_cause)
    private TextView text_cause;

    /**
     * 输入地址
     */
    @ViewInject(R.id.aty_replenish_et_address)
    private TextView text_address;

    /**
     * 补录天数
     */
    private String repDays;

    /**
     * 时间选择器
     */
    private LsfbTimePicker timePicker;
    private Intent intent;
    private TextView textView;

    /**
     * 地址id
     */
    private int addr_id = 0;
    private int rid = 0;

    private MyProgressDialog dialog;

    @Override
    public void initView() {
        LsfbTitleBar titleBar = LittleUtils.setsimpletitlebar(this, "定位补录");
        textView = LittleUtils.rightbtn(this, "提交");

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(ReplenishSignInActivity.this, ReplenishSignInActivity.class));
                replenish();
            }
        });
        titleBar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

        dialog = new MyProgressDialog(this, "加载中...");

        timePicker = new LsfbTimePicker(this, 0);
        timePicker.SelectSureListenner(new LsfbTimePicker.ClickSureListener() {
            @Override
            public void ClickSure(String time) {
                text_time.setText(time);
            }
        });

    }

    @Override
    public void Resume() {
//        getMyInfo();
    }

    /**
     * 提交补录申请
     */
    private void replenish() {
        if (text_time.getText().toString().length() == 0) {
            T.showShort(this, "请选择时间");
            return;
        }
        if (rid == 0) {
            T.showShort(this, "请选择补录原因");
            return;
        }
        if (addr_id == 0) {
            T.showShort(this, "请输入地址");
            return;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("address", String.valueOf(addr_id));
        if (text_cause.getText().toString().length() != 0) {
            hashMap.put("content", text_cause.getText().toString());
        }
        hashMap.put("time", text_time.getText().toString());
        hashMap.put("rid", String.valueOf(rid));
        dialog.showdialog();
        new BaseInternet().getData(URLString.REPSIGNAPPLY, hashMap, String.class, new ReplenishApplyEvent<String>(), true);
    }

    @EventAnnotation
    public void eventResult(ReplenishApplyEvent<String> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                T.showShort(this, "补录申请提交成功");
                this.finish();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 获取我的个人信息
     */
    private void getMyInfo() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        dialog.showdialog();
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyInfoEvent<MyDetailsBean>(), true, false);
    }

    @EventAnnotation
    public void MyInfoEvent(MyInfoEvent<MyDetailsBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                repDays = event.getData().getRepDays();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_replenish_time, R.id.aty_replenish_cause, R.id.aty_replenish_et_address})
    public void onClick(View view) {
        switch (view.getId()) {
            //选择时间
            case R.id.aty_replenish_time:
                timePicker.showTimePickerView(this, Integer.parseInt(repDays));
                break;
            //选择原因
            case R.id.aty_replenish_cause:
                intent = new Intent(ReplenishSignInActivity.this, ReplenishReasonActivity.class);
                startActivityForResult(intent, 0);
                break;
            //出差地址
            case R.id.aty_replenish_et_address:
                intent = new Intent(ReplenishSignInActivity.this, SearchAddressActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    public void initData() {
        getMyInfo();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        } else {
            switch (requestCode) {
                case 0: //原因
                    ReasonBean.ListBean datas1 = (ReasonBean.ListBean) data.getSerializableExtra("datas");
                    text_cause.setText(datas1.getContent());
                    rid = datas1.getId();
                    break;
                case 1: //地址
                    SearchCityBean.ListBean datas2 = (SearchCityBean.ListBean) data.getSerializableExtra("datas");
                    text_address.setText(datas2.getCityName());
                    addr_id = datas2.getId();
                    break;
            }
        }
    }
}
